package org.ellisande.grid.tags;

import javax.servlet.jsp.JspWriter;

import org.ellisande.grid.tags.support.TagBuilder;
import org.ellisande.tld.annotations.Attribute;
import org.ellisande.tld.annotations.Description;
import org.ellisande.tld.annotations.Info;
import org.ellisande.tld.annotations.Tag;


@Tag
@Info("")
public class LabelTag extends AbstractTag {

	private static enum Type{
		DEFAULT(""),CHECKBOX("checkbox");
		
		private String type;
		
		private Type(String type){
			this.type = type;
		}
		
		public String style(){
			return type;
		}
	}
	
	private static final long serialVersionUID = 4119135671557342256L;
	
	private static final String LABEL_STYLE = "control-label";
	
	@Attribute
	@Description("Defines the type of the label. For all input elements except checkboxes this will be default. Allowed values: default | checkbox")
	private Type type = Type.DEFAULT;
	
	@Attribute
	@Description("The name of the input this label is associated with.")
	private String forName = "";

	@Override
	public void startTag() throws Exception {
		TagBuilder label = createTagBuilder("label");
		label.addStyle(LABEL_STYLE);
		label.addStyle(type.style());
		if(!forName.isEmpty()){
			label.addAttribute("for", forName);
		}
		
		JspWriter out = pageContext.getOut();
		out.write(label.compileStartTag());

	}

	@Override
	public void endTag() throws Exception {
		JspWriter out = pageContext.getOut();
		out.write("</label>");

	}
	
	@Override
	public void release(){
		super.release();
		forName = null;
		type = null;
	}
	
	public void setForName(String name){
		this.forName = name;
	}

}
